package com.dbh.alg.datastructure.stack;

import java.util.LinkedList;

/**
 * 逆波兰运算
 * @author dongbanghui
 * @date 2025/5/22
 */
public class E02Leetcode150 {

    public static int evalRPN(String[] tokens) {
        LinkedList<Integer> stack = new LinkedList<>();
        for (String s : tokens) {
            switch (s) {
                case "+": {
                    Integer b = stack.pop();
                    Integer a = stack.pop();
                    Integer c = a + b;
                    stack.push(c);
                    break;
                }
                case "-": {
                    Integer b = stack.pop();
                    Integer a = stack.pop();
                    Integer c = a - b;
                    stack.push(c);
                    break;
                }

                case "*": {
                    Integer b = stack.pop();
                    Integer a = stack.pop();
                    Integer c = a * b;
                    stack.push(c);
                    break;
                }

                case "/": {
                    Integer b = stack.pop();
                    Integer a = stack.pop();
                    Integer c = a / b;
                    stack.push(c);
                    break;
                }
                default:{
                    stack.push(Integer.parseInt(s));
                }

            }
        }
        return stack.pop();
    }

    public static void main(String[] args) {
        String[] tokens ={"2","1","+","3","*"};
        System.out.println(E02Leetcode150.evalRPN(tokens));
    }
}
